package com.eshop.modules.quartz.service.impl;

import java.util.Collection;
import java.io.Serializable;
import com.eshop.exception.BadRequestException;
import java.io.IOException;
import java.util.Iterator;
import com.eshop.utils.FileUtil;
import java.util.ArrayList;
import javax.servlet.http.HttpServletResponse;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.eshop.common.utils.QueryHelpPlus;
import com.eshop.modules.quartz.service.dto.QuartzJobDto;
import java.util.LinkedHashMap;
import java.util.List;
import com.github.pagehelper.PageInfo;
import java.util.Map;
import org.springframework.data.domain.Pageable;
import com.eshop.modules.quartz.service.dto.QuartzJobQueryCriteria;
import com.eshop.modules.quartz.utils.QuartzManage;
import com.eshop.dozer.service.IGenerator;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.stereotype.Service;
import com.eshop.modules.quartz.service.QuartzJobService;
import com.eshop.modules.quartz.domain.QuartzJob;
import com.eshop.modules.quartz.service.mapper.QuartzJobMapper;
import com.eshop.common.service.impl.BaseServiceImpl;

@Service
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = { Exception.class })
public class QuartzJobServiceImpl extends BaseServiceImpl<QuartzJobMapper, QuartzJob> implements QuartzJobService
{
    private final IGenerator generator;
    private final QuartzManage quartzManage;
    
    public Map<String, Object> queryAll(final QuartzJobQueryCriteria criteria, final Pageable pageable) {
        this.getPage(pageable);
        final PageInfo<QuartzJob> page = (PageInfo<QuartzJob>)new PageInfo((List)this.queryAll(criteria));
        final Map<String, Object> map = new LinkedHashMap<String, Object>(2);
        map.put("content", this.generator.convert(page.getList(), (Class)QuartzJobDto.class));
        map.put("totalElements", page.getTotal());
        return map;
    }
    
    public List<QuartzJob> queryAll(final QuartzJobQueryCriteria criteria) {
        return (List<QuartzJob>)((QuartzJobMapper)this.baseMapper).selectList(QueryHelpPlus.getPredicate(QuartzJob.class, criteria));
    }
    
    public void download(final List<QuartzJobDto> all, final HttpServletResponse response) throws IOException {
        final List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
        for (final QuartzJobDto quartzJob : all) {
            final Map<String, Object> map = new LinkedHashMap<String, Object>();
            map.put("Spring Bean名称", quartzJob.getBeanName());
            map.put("cron 表达式", quartzJob.getCronExpression());
            map.put("状态：1暂停、0启用", quartzJob.getIsPause());
            map.put("任务名称", quartzJob.getJobName());
            map.put("方法名称", quartzJob.getMethodName());
            map.put("参数", quartzJob.getParams());
            map.put("备注", quartzJob.getRemark());
            map.put("创建日期", quartzJob.getCreateTime());
            map.put("Spring Bean名称", quartzJob.getBeanName());
            map.put("cron 表达式", quartzJob.getCronExpression());
            map.put("状态：1暂停、0启用", quartzJob.getIsPause());
            map.put("任务名称", quartzJob.getJobName());
            map.put("方法名称", quartzJob.getMethodName());
            map.put("参数", quartzJob.getParams());
            map.put("备注", quartzJob.getRemark());
            map.put("创建日期", quartzJob.getCreateTime());
            map.put("Spring Bean名称", quartzJob.getBeanName());
            map.put("cron 表达式", quartzJob.getCronExpression());
            map.put("状态：1暂停、0启用", quartzJob.getIsPause());
            map.put("任务名称", quartzJob.getJobName());
            map.put("方法名称", quartzJob.getMethodName());
            map.put("参数", quartzJob.getParams());
            map.put("备注", quartzJob.getRemark());
            map.put("创建日期", quartzJob.getCreateTime());
            list.add(map);
        }
        FileUtil.downloadExcel((List)list, response);
    }
    
    public void updateIsPause(final QuartzJob quartzJob) {
        if (quartzJob.getId().equals(1L)) {
            throw new BadRequestException("该任务不可操作");
        }
        if (quartzJob.getIsPause()) {
            this.quartzManage.resumeJob(quartzJob);
            quartzJob.setIsPause(false);
        }
        else {
            this.quartzManage.pauseJob(quartzJob);
            quartzJob.setIsPause(true);
        }
        this.saveOrUpdate(quartzJob);
    }
    
    public boolean save(final QuartzJob quartzJob) {
        this.quartzManage.addJob(quartzJob);
        return this.retBool(Integer.valueOf(((QuartzJobMapper)this.baseMapper).insert(quartzJob)));
    }
    
    public boolean updateById(final QuartzJob quartzJob) {
        this.quartzManage.updateJobCron(quartzJob);
        return this.retBool(Integer.valueOf(((QuartzJobMapper)this.baseMapper).updateById(quartzJob)));
    }
    
    public void execution(final QuartzJob quartzJob) {
        if (quartzJob.getId().equals(1L)) {
            throw new BadRequestException("该任务不可操作");
        }
        this.quartzManage.runJobNow(quartzJob);
    }
    
    public List<QuartzJob> findByIsPauseIsFalse() {
        final QuartzJobQueryCriteria criteria = new QuartzJobQueryCriteria();
        criteria.setIsPause(false);
        return (List<QuartzJob>)((QuartzJobMapper)this.baseMapper).selectList(QueryHelpPlus.getPredicate(QuartzJob.class, criteria));
    }
    
    public void removeByIds(final List<Integer> idList) {
        idList.forEach(id -> {
            QuartzJob quartzJob = (QuartzJob)((QuartzJobMapper)this.baseMapper).selectById((Serializable)id);
            this.quartzManage.deleteJob(quartzJob);
            return;
        });
        ((QuartzJobMapper)this.baseMapper).deleteBatchIds((Collection)idList);
    }
    
    public QuartzJobServiceImpl(final IGenerator generator, final QuartzManage quartzManage) {
        this.generator = generator;
        this.quartzManage = quartzManage;
    }
}
